package com.stj.servlet;

import com.google.gson.Gson;
import com.stj.entity.Blog;
import com.stj.entity.comment;
import com.stj.service.BlogService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/blogServlet")
public class BogServlet extends HttpServlet {

    BlogService blogService=new BlogService();
    /* 拦截 */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String way = req.getParameter("way");
        if(way.equals("getBlogAll")){
            this.getBlogAll(req,resp);
        }else if(way.equals("getBLogById")){
            this.getBlogbyId(req,resp);
        }else if(way.equals("search")){
            this.geTSearchByName(req,resp);
        }else if(way.equals("add")){
            this.addBlog(req,resp);
        }else if(way.equals("comment")){
            this.getComment(req,resp);
        }else if(way.equals("blogType")){
               this.getBlogType(req,resp);
        }
    }

    private void getBlogType(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        resp.setContentType("text/html; charset=utf-8");
        String typeId = req.getParameter("typeId");
        List blogTypeList = blogService.getBlogTypeList(Integer.parseInt(typeId));
        HttpSession session = req.getSession();
        session.setAttribute("blogTypeList",blogTypeList);
        req.getRequestDispatcher("/foreground/blog/blogType.jsp").forward(req,resp);
    }

    /*发表评论*/
    private void getComment(HttpServletRequest req, HttpServletResponse resp)throws  IOException,ServletException {
        String content = req.getParameter("content");
        String verify = req.getParameter("verify");
        String id = req.getParameter("blog.id");
        int replyHit = Integer.parseInt(req.getParameter("replyHit"));
        String userIp = "0:0:0:0:1:1:1:1";
        comment comment = new comment(Integer.parseInt(id),content,userIp,0);
        int i =blogService.getcomment(comment);
        blogService.addReplyHit(Integer.parseInt(id),replyHit);
        String json =null;
        if(i>0){
            Gson gson = new Gson();
            json= gson.toJson("success");
        }
        resp.getWriter().write(json);
    }

    /*发表博文*/
    public void addBlog(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String title = req.getParameter("title");
        String blogTypeId = req.getParameter("blogType.id");
        String content = req.getParameter("content");
        String summary = req.getParameter("summary");
        Blog blog = new Blog(title,summary,content,Integer.parseInt(blogTypeId));
        int i = blogService.addBlog(blog);
        if(i>0){
            Gson gson = new Gson();
            String json = gson.toJson("success");
            resp.getWriter().write(json);
        }
    }

    /*搜索*/
    private void geTSearchByName(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String q = req.getParameter("search");
        List blogList = blogService.getBlogByNameList(q);
        HttpSession session = req.getSession();
        session.setAttribute("blogList",blogList);
        session.setAttribute("q",q);
        //转发到result
        req.getRequestDispatcher("/foreground/blog/result.jsp").forward(req, resp);
    }

    /*通过id查找博客*/
    private void getBlogbyId(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
            String id = req.getParameter("id");
            Blog blog = blogService.getBlogByIdList(id);
            List commentList = blogService.selectComment(Integer.parseInt(id));
            HttpSession session = req.getSession();
            session.setAttribute("blog",blog);
            session.setAttribute("commentList",commentList);
            req.getRequestDispatcher("/foreground/blog/view.jsp").forward(req,resp);
    }

    //查询所有的博文列表
    private void getBlogAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List blogList = blogService.getBlogList();
        List blogTypeCountList = blogService.blogTypeCountList();
        List linkList = blogService.linkList();
        HttpSession session = req.getSession();
        //真正需要数据得是list.jsp页面
        session.setAttribute("blogList",blogList);
        //按日志类别
        session.setAttribute("blogTypeCountList",blogTypeCountList);
        //友情链接
        session.setAttribute("linkList",linkList);
        //转发到首页
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

}
